@RestResource(urlMapping='/account/*')
global with sharing class UserService {

    // 使用 HttpGet 注解处理 GET 请求
    @HttpGet
    global static void getAccountByPhoneNumber() {
        // 获取 RestRequest 对象
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        
        // 从请求参数中获取 phone 参数
        String phoneNumber = req.params.get('phone');
        
        // 初始化响应状态码为 200（OK）
        res.statusCode = 200;
        
        // 检查 phoneNumber 是否为空
        if (String.isBlank(phoneNumber)) {
            res.statusCode = 400; // 400 Bad Request
            res.responseBody = Blob.valueOf(JSON.serialize(new Map<String, String>{
                'error' => 'Phone number is required.'
            }));
            return;
        }
        
        // 查询联系人信息（假设使用 Contact 对象，字段为 Phone）
        List<Account> accounts = [SELECT Name, Phone FROM Account WHERE Phone = :phoneNumber LIMIT 1];
        
        // 如果未找到联系人，设置 404 状态码并返回错误信息
        if (accounts.isEmpty()) {
            res.statusCode = 404; // 404 Not Found
            res.responseBody = Blob.valueOf(JSON.serialize(new Map<String, String>{
                'error' => 'User not found.'
            }));
            return;
        }
        
        // 如果找到联系人，返回联系人信息
        // 这里只返回第一个匹配的结果
        Account account = accounts[0];
        Map<String, Object> result = new Map<String, Object>{
            'Name' => account.Name,
            'Phone' => account.Phone
        };
        
        res.responseBody = Blob.valueOf(JSON.serialize(result));
    }

    @HttpPost
    global static String createAccount() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        
        // 从请求体中获取 JSON 数据
        String requestBody = req.requestBody.toString();
        Map<String, Object> data = (Map<String, Object>) JSON.deserializeUntyped(requestBody);
        
        // 获取手机号
        String phone = (String) data.get('Phone');
        if (String.isBlank(phone)) {
            res.statusCode = 400;
            return JSON.serialize(new Map<String, String>{'error' => 'Phone number is required.'});
        }
        
        // 创建新的 Account 记录
        Account acc = new Account();
        acc.Phone = phone;
        acc.Name = (String) data.get('Name'); // Name 是必填字段
        // 你可以根据需要添加更多字段
        // 例如：acc.Industry = (String) data.get('Industry');
        
        try {
            insert acc;
            res.statusCode = 201; // 201 Created
            return JSON.serialize(new Map<String, String>{
                'Id' => acc.Id,
                'Phone' => acc.Phone,
                'Name' => acc.Name
            });
        } catch (DmlException e) {
            res.statusCode = 400;
            return JSON.serialize(new Map<String, String>{'error' => e.getMessage()});
        }
    }

    @HttpPut
    global static String updateAccount() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        
        // 从请求参数中获取 phone 参数
        String phone = req.params.get('phone');
        if (String.isBlank(phone)) {
            res.statusCode = 400;
            return JSON.serialize(new Map<String, String>{'error' => 'Phone number is required.'});
        }
        
        // 从请求体中获取 JSON 数据
        String requestBody = req.requestBody.toString();
        Map<String, Object> data = (Map<String, Object>) JSON.deserializeUntyped(requestBody);
        
        // 查询要更新的 Account 记录
        List<Account> accounts = [SELECT Id, Phone, Name FROM Account WHERE Phone = :phone LIMIT 1];
        if (accounts.isEmpty()) {
            res.statusCode = 404;
            return JSON.serialize(new Map<String, String>{'error' => 'Account not found.'});
        }
        
        Account acc = accounts[0];
        // 更新字段
        if (data.containsKey('Name')) {
            acc.Name = (String) data.get('Name');
        }
        // 你可以根据需要添加更多字段
        // 例如：if (data.containsKey('Industry')) { acc.Industry = (String) data.get('Industry'); }
        
        try {
            update acc;
            res.statusCode = 200; // 200 OK
            return JSON.serialize(new Map<String, String>{
                'Id' => acc.Id,
                'Phone' => acc.Phone,
                'Name' => acc.Name
            });
        } catch (DmlException e) {
            res.statusCode = 400;
            return JSON.serialize(new Map<String, String>{'error' => e.getMessage()});
        }
    }

    @HttpDelete
    global static String deleteAccount() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        
        // 从请求参数中获取 phone 参数
        String phone = req.params.get('phone');
        if (String.isBlank(phone)) {
            res.statusCode = 400;
            return JSON.serialize(new Map<String, String>{'error' => 'Phone number is required.'});
        }
        
        // 查询要删除的 Account 记录
        List<Account> accounts = [SELECT Id FROM Account WHERE Phone = :phone LIMIT 1];
        if (accounts.isEmpty()) {
            res.statusCode = 404;
            return JSON.serialize(new Map<String, String>{'error' => 'Account not found.'});
        }
        
        try {
            delete accounts[0];
            res.statusCode = 200; // 200 OK
            return JSON.serialize(new Map<String, String>{'message' => 'Account deleted successfully.'});
        } catch (DmlException e) {
            res.statusCode = 400;
            return JSON.serialize(new Map<String, String>{'error' => e.getMessage()});
        }
    }
}